package org.cloudie.docs.editor.client.windows;

import org.cloudie.docs.editor.client.commands.generic.ExplorerWindowOpenDocumentCommand;
import org.cloudie.docs.editor.client.commands.generic.ExplorerWindowOpenFolderCommand;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.feeds.docs.FileServiceEntry;
import org.cloudie.docs.editor.client.icons.explorer.document.DocumentFolderExplorerIcon;
import org.cloudie.docs.editor.client.icons.explorer.document.DocumentShortcutExplorerIcon;
import org.cloudie.docs.editor.client.resources.icons.IconsHelper;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Widget;

public class DocumentExplorerWindow extends ExplorerWindow<FileServiceEntry> {
  
  public DocumentExplorerWindow() {
    super("Document Explorer", new String[] { "Name", "Type", "Modified" }, true);
  }
  
  @Override
  public void show(FileServiceEntry folder, FileServiceEntry[] entries) {
	super.show(folder, entries);
	if (folder.getExplorerEntryId() != null) {
	  DocumentFolderExplorerIcon folderIcon =
		  new DocumentFolderExplorerIcon(folder.getExplorerEntryParentId(),
			  "..",
		      "filesystems/folder",
		      "Open parent folder",
		      new ExplorerWindowOpenFolderCommand(folder.getExplorerEntryParentId(), true));
		    folderIcon.setMovable(false);
		    addEntry(folderIcon, new String[] { "..", "", "" });
	}
	for(FileServiceEntry entry : entries) {
	  addEntry(entry);
	}
    scrollTable.setColumnWidth(0, 160);
    scrollTable.setColumnWidth(1, 60);
	show();
  }
  
  protected void addEntry(FileServiceEntry entry) {
    String folderId = currentFolder.getExplorerEntryId();
	if (folderId == null && entry.getFolders().length > 0 ||
		folderId != null && entry.getFolders().length == 0) {
	  return;
	} else if(folderId != null &&
	  !folderId.equals(entry.getFolders()[0])){
	  return;
	}
	String type = "File", icon = IconsHelper.getMimetypeIconIdentifier(entry.getType(), true);
	if (entry.getType().equalsIgnoreCase("folder")) {
      type = "Folder";
	  DocumentFolderExplorerIcon folderIcon =
	    new DocumentFolderExplorerIcon(entry.getExplorerEntryId(),
	       entry.getExplorerEntryLabel(), icon,
	       "Open " + entry.getExplorerEntryLabel(),
	        new ExplorerWindowOpenFolderCommand(entry.getTitle(), true));
	    addEntry(folderIcon,
	  	  new String[] {entry.getTitle(), type,
	    	DateTimeFormat.getShortDateTimeFormat().format(entry.getEdited())});
	} else {
      if (entry.getType().equalsIgnoreCase("document")) {
		type = "Document";
      } else if (entry.getType().equalsIgnoreCase("spreadsheet")) {
		type = "Spreadsheet";
	  } else if (entry.getType().equalsIgnoreCase("presentation")) {
		type = "Presentation";
	  } else if (entry.getType().equalsIgnoreCase("drawing")) {
		type = "Drawing";
	  } else if (entry.getType().equalsIgnoreCase("desktop")) {
		type = "Desktop";
	  }
      DocumentShortcutExplorerIcon fileIcon =
        new DocumentShortcutExplorerIcon(entry.getExplorerEntryId(), entry.getExplorerEntryLabel(),
          icon, "Open " + entry.getExplorerEntryLabel(),
    	  new ExplorerWindowOpenDocumentCommand(entry.getDocumentId()));
      addEntry(fileIcon, new String[] {entry.getTitle(), type,
          DateTimeFormat.getShortDateTimeFormat().format(entry.getEdited()) });
	}
  }

  @Override
  public void refresh() {
	clearEntries();
    CommandEvent.fire(new ExplorerWindowOpenFolderCommand(currentFolder.getExplorerEntryId(), false));
  }
  
  @Override
  public boolean acceptsReceives(Widget dropped, boolean isFinal) {
    return false;
  }
  
  @Override
  public boolean acceptsReceives() {
    return false;
  }
  
  @Override
  public void onReceive(Widget dropped, int x, int y) {
  }

}
